/**
 * 
 */
package es.aradesh.ministryassist.gui.ministry;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.AdapterView.AdapterContextMenuInfo;
import es.aradesh.ministryassist.R;
import es.aradesh.ministryassist.db.DbAdapter;
import es.aradesh.ministryassist.logic.ministry.Home;
import es.aradesh.ministryassist.logic.ministry.HomeBuilder;
import es.aradesh.ministryassist.logic.ministry.MinistryElement;
import es.aradesh.ministryassist.logic.ministry.Note;
import es.aradesh.ministryassist.logic.ministry.NoteBuilder;

/**
 * @author xmorales
 * 
 */
public class HomeDetailsActivity extends Activity {
	private DbAdapter mDb;
	private Home home;
	private List<Note> notes;
	private Note contextPointer;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mDb = new DbAdapter(this);
		mDb.open();

		Bundle extras = getIntent().getExtras();
		if (extras.isEmpty()) {
			finish();
		} else {
			setContentView(R.layout.show_home);
			home = HomeBuilder.getHomeById(mDb, extras
					.getLong(BaseColumns._ID));
			notes = NoteBuilder.getNotesByHome(mDb, home);

			Button doneButton = (Button) findViewById(R.id.show_done);
			TextView temp = (TextView) findViewById(R.id.show_name);
			ListView notesList = (ListView) findViewById(R.id.notesList);
			registerForContextMenu(notesList);
			Spinner status = (Spinner) findViewById(R.id.show_status);
			
			// Set name
			temp.setText(home.getStreet().getTerritory() + " "
					+ home.getStreet() + " num." + home);
			// Set owner
			temp = (TextView) findViewById(R.id.show_owner);
			temp.setText(home.getOwner());
			if (notes.isEmpty())
				notesList.setAdapter(new ArrayAdapter<String>(this,
						android.R.layout.simple_list_item_1,
						new String[] { this.getString(R.string.noElements) }));
			else
				notesList.setAdapter(new ArrayAdapter<Note>(this,
						android.R.layout.simple_list_item_1, notes));
			status.setSelection(home.getStatus());
			
			doneButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					if (((EditText)findViewById(R.id.show_add_note)).getText().toString() != ""){
						if (((ToggleButton) findViewById(R.id.addPubliButton))
								.isChecked()) {
							int type = 1 + ((Spinner) findViewById(R.id.show_Publi))
									.getSelectedItemPosition();
							NoteBuilder.createNote(mDb, home, ((EditText)findViewById(R.id.show_add_note)).getText().toString(), type);
						} else
							NoteBuilder.createNote(mDb, home, ((EditText)findViewById(R.id.show_add_note)).getText().toString(), Note.TYPE.NOTA
									.ordinal());
					}
					home.setStatus(((Spinner)findViewById(R.id.show_status)).getSelectedItemPosition());
					home.refreshDB(mDb);
					Intent mIntent = new Intent();
					setResult(RESULT_OK, mIntent);
					finish();
				}
			});
		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		Intent i = new Intent(this, AddEditActivity.class);
		switch (item.getItemId()) {
		case MinistryElement.MENU_EDIT:
			i.putExtra(MinistryElement.OPERATION,
					contextPointer.getActivityEditType());
			i.putExtra(BaseColumns._ID, contextPointer.getId());
			startActivityForResult(i, contextPointer.getActivityEditType());
			break;
		case MinistryElement.MENU_DELETE:
			contextPointer.delete(mDb);
			notes = NoteBuilder.getNotesByHome(mDb, home);
			//((ListView) findViewById(R.id.notesList)).setAdapter(new ArrayAdapter<Note>());
//			onActivityResult(contextPointer.getActivityEditType(), RESULT_OK, i);
			break;
		}
		return super.onContextItemSelected(item);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu,
	 *      android.view.View, android.view.ContextMenu.ContextMenuInfo)
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
			//menu.add(0, IConstants.MENU_EDIT, 0, R.string.edit);
			menu.add(0, MinistryElement.MENU_DELETE, 1, R.string.delete);
			contextPointer = (Note) ((ListView) v)
					.getItemAtPosition(new Long(info.id).intValue());
			super.onCreateContextMenu(menu, v, menuInfo);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mDb.close();
	}
	
	public void onPubliButtonClick(View view){
	ToggleButton publi = (ToggleButton)findViewById(R.id.addPubliButton);
	if (publi.isChecked()){
		findViewById(R.id.show_Publi).setVisibility(View.VISIBLE);
		((ListView)findViewById(R.id.notesList)).setVisibility(View.INVISIBLE);
	}else{
		findViewById(R.id.show_Publi).setVisibility(View.GONE);
		((ListView)findViewById(R.id.notesList)).setVisibility(View.VISIBLE);
	}
	}
}
